package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.core.app.ActivityCompat.startActivityForResult
import androidx.lifecycle.LiveData
import androidx.lifecycle.Transformations
import androidx.paging.PagedList
import com.cheyun.netsalev3.bean.Row
import com.cheyun.netsalev3.repository.HomeCustomerPageRepository
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.view.defeatdetails.DefeatDetailsActivity

/**
 * @author Michael
 * @date  2018/12/10 - 10:56.
 */
class DefeatAuditActivityViewMoldel(var repository: HashMap<String, HomeCustomerPageRepository>) : BaseViewModel() {
    var pagedlist = HashMap<String, LiveData<PagedList<Row>>>()
    var nkeys = arrayOf("archivecheck.fail", "archivecheck.invalid")
    var nkey = ""
    var hashMap = HashMap<String, LiveData<NetworkState>>()


    init {
        for (key in repository.keys) {
            repository[key]?.also {
                pagedlist[key] = it.loadAndroidData()
            }
        }

    }

    init {
        for (item in nkeys) {
            pagedlist[item]?.apply {
                hashMap[item] = Transformations.switchMap(this) { repository[item]!!.networkState }!!
            }

        }

    }

    /**
     * 点击item
     */
    fun onClickItem(view: View, item: Row) {
        var intent = Intent(view.context, DefeatDetailsActivity::class.java)
        intent.putExtra("tid", item.tid)
        intent.putExtra("nkey", nkey)
        var activity = view.context as Activity
        startActivityForResult(activity,intent,1,null)
        //view.context.startActivityForResult(intent, REQUESTCODE);
//        view.context.startActivity(intent)
    }

    /**
     *设置当前选中的nkey
     */
    fun setPosition(nkey: String) {
        this.nkey = nkey
    }

    /**
     * 刷新
     */
    fun refresh(nkey: String) {
        repository[nkey]?.dataSourceFactory?.sourceLiveData?.value?.invalidate()
    }
}